/*============================================================================*/
/*                        Tortoise Team			                              */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* C Source:         %tHAL_DcMotor.c%
* Instance:         RPL_1
* %version:         1 %
* %created_by:      Jose Antonio Leon Jazo %
* %date_created:    Sunay September 23 13:24:00 2012 %
*=============================================================================*/
/* DESCRIPTION : C source template file                                       */
/*============================================================================*/
/* FUNCTION COMMENT : Contains the function defitions of the hardware		  */
/* layer of the dc motor	                                                  */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.0      | 23/09/2012  |                               | Antonio Leon     */
/* Integration under Continuus CM                                             */
/*============================================================================*/

/* Includes */
/* -------- */
#include "stdtypedef.h"
#include "MAL_EMIOS.h"
#include "MAL_IOAPI.h"
#include "MAL_PWM.h"
#include "HAL_DcMotor.h"

/* Functions macros, constants, types and datas         */
/* ---------------------------------------------------- */
/* Functions macros */

/*==================================================*/ 
/* Definition of constants                          */
/*==================================================*/ 


/*======================================================*/ 
/* Definition of RAM variables                          */
/*======================================================*/ 

/*======================================================*/ 
/* close variable declaration sections                  */
/*======================================================*/ 

/* Private defines */

/* \def Motor_Period_Us
Period of the motor in us
*/
#define Motor_Period_Us		20000

/* \def Motors_MCB_Channel
Chanel of the counter
*/
#define Motors_MBC_Channel	0		

/* \def Counter_Bus
The bus's counter of the pwm
*/
#define Counter_Bus			COUNTER_BUS_BCDE

/* Private functions prototypes */
/* ---------------------------- */


/* Exported functions prototypes */
/* ----------------------------- */
void vfnInit_DcMotor(void);
void vfnSet_DcMotor(T_UWORD uw_speed, T_UWORD uw_minVal, T_UWORD uw_maxVal, T_UWORD uw_DcChan);

/* Inline functions */
/* ---------------- */

/* Private functions */
/* ----------------- */

/* Exported functions */

/**************************************************************
 *  Name                 :	vfInit_DcMotor
 *  Description          :	Initializing Dc motors
 *  Parameters           :  [Input, Output, Input / output]
 *  Return               :	void
 *  Critical/explanation :  No
 **************************************************************/

void vfnInit_DcMotor(void)
{	
	IO_SetPinFctn(pin_PB0, PA_GPIO);
	IO_SetPinAsOutput(pin_PB0,1);
	
	IO_SetPinFctn(pin_PB1, PA_GPIO);
	IO_SetPinAsOutput(pin_PB1,1);
	
	IO_PinWrite(pin_PB0, 1);
	IO_PinWrite(pin_PB1, 1);
	
	IO_ChangeFctnPin(16, 1);
	IO_ChangeFctnPin(17, 1);
	
	vfnSetUpClock();
	vfnInitPWMChannel(Dc1_Channel, StartS_Us, Motors_MBC_Channel, Motor_Period_Us, Counter_Bus, Dc1_Pin);
	vfnInitPWMChannel(Dc2_Channel, StartS_Us, Motors_MBC_Channel, Motor_Period_Us, Counter_Bus, Dc2_Pin);
	//START_COUNTING();
}

/**************************************************************
 *  Name                 :	vfSet_DcMotor
 *  Description          :	Seting the speed of the Dc motor
 *  Parameters           :  [Input:uw_speed: speed of the motor; uw_minVal: Lower limit; uw_maxVal:Highest limit; uw_dcChannel: Selecting motor
 							Output, Input / output]
 *  Return               :	void
 *  Critical/explanation :  No
 **************************************************************/
void vfnSet_DcMotor(T_UWORD uw_speed, T_UWORD uw_minVal, T_UWORD uw_maxVal, T_UWORD uw_dcChannel)
{
	T_UWORD uw_duty;
	
	if(uw_speed < uw_minVal)
	{
		uw_speed = uw_minVal;
	}
	else if(uw_speed > uw_maxVal)
	{
		uw_speed = uw_maxVal;
	}
	
	uw_duty = uw_speed;
	
	if(uw_dcChannel == Dc1_Channel)
	{
		 vfnSetDutyCycle(Dc1_Channel, uw_duty);
	}
	else
	{
		vfnSetDutyCycle(Dc2_Channel, uw_duty);
	}
}

/* ------------------ */
